<?php
	/**
	 *	Copyright (c) TeamFlamingo And gray 2004-2009
	 *	www.me-live.com.cn / www.i-gray.cn
	 *
	 *	[ WARNING ]
	 *		This is NOT a freeware!
	 *		You MUST get author's authorization before using it!
	 *	[ WARNING ]
	 *
	 *	Name : template.h.php / Development Code Evolve
	 *	Created / Modify : 2010-2-15 / --
	 */

	if( !defined( 'IN_TEAME' ) )
	{
		exit( 'Access Denied' );
	}

	if( !is_dir( ME_CACHE_DIR ) )
	{
		@mkdir( ME_CACHE_DIR, 0777 );
	}

	function AjaxTemplate ( $tplFile, $tplSystem = false )
	{
		$tplSystem	=	$tplSystem ? $tplSystem : 'default';
		$currentTplFile		=	ME_APP . "/templates/{$tplSystem}/{$tplFile}.tpl.html";
		$defaultTplFile		=	ME_APP . "/templates/default/{$tplFile}.tpl.html";

		if( !file_exists( $currentTplFile ) )
		{
			return	file_exists( $defaultTplFile ) ? $defaultTplFile : '';
		}
		return	$currentTplFile;
	}

	function template ( $tplFile, $tplSystem = false )
	{
		$tplSystem	=	$tplSystem ? $tplSystem : 'default';

		$phpCacheFile		=	ME_CACHE_DIR . "/__{$tplSystem}.{$tplFile}.c.php";
		$currentTplFile		=	ME_APP . "/templates/{$tplSystem}/{$tplFile}.tpl.html";
		$defaultTplFile		=	ME_APP . "/templates/default/{$tplFile}.tpl.html";


		if( !file_exists( $currentTplFile ) )
		{
			if( !file_exists( $defaultTplFile ) )
			{
				$Err	=	debug_backtrace();
				echo	"<span class='Err'><a>Error</a>:Function call mistake;</span><br /><span class='Err'>The error in the <u>{$Err[0]['file']}</u> file <b>{$Err[0]['line']}</b> lines;</span><br /><span class='Err'>The <u>{$tplFile}.tpl.html</u> is not found;</span>";
				return	'';
			}
			else
			{
				$tplFile	=	$defaultTplFile;
			}
		}
		else
		{
			$tplFile	=	$currentTplFile;
		}

		if( !file_exists( $phpCacheFile ) || ( time() - filemtime( $phpCacheFile ) ) > ME_CACHE_LIFE )
		{
			return	createCacheFile( $tplFile, $phpCacheFile );
		}

		return	$phpCacheFile;
	}

	function createCacheFile( $tplFile, $phpCacheFile )
	{
		$file	=	@file_get_contents( $tplFile );

		//$file	=	preg_replace( "/\|/m", "||", $file );
		$file	=	preg_replace( "/\<\!\-\-\{(.+?)\}\-\-\>/s", "<?php \$1?>", $file );
		//	[CH]	这里做了一个简单的修改,因为发现无法调用 {time()}这样格式的函数
		//	[CH]	2010-2-22
		$file	=	preg_replace( "/\{(\w+?)\((.*?)\)\}/s", "<?php echo $1(\$2)?>", $file );
		$file	=	preg_replace( "/\{\\$(.+?)\}/s", "<?php echo $\$1?>", $file );
		$file	=	preg_replace( "/\{#import\s+\'(\w+)\'\}/i", '<?php include template(\'$1\')?>', $file );
		$file	=	preg_replace( "/\<\!\-\-(.+?)\-\-\>/s", "", $file );
		//$file	=	preg_replace( "/\s+/m", ' ', $file );
		$file	=	preg_replace( "/^\s+|\s+$/m", '', $file );
		$file	=	preg_replace( "/\t+/s", ' ', $file );
		//$file	=	preg_replace( "/([\n\r|\n|\r|\t]+)/s", "", $file );
		//$file	=	preg_replace( "/([^\/\/(\s*)\<\!\[CDATA\[\n|\r|\n\r])\n|\r|\n\r|^\/\/.*\n|\r|\n\r$/m", '$1', $file );
		$file	=	preg_replace( "/^\/\/.*\n|\r|\n\r$/m", '', $file );
		$file	=	preg_replace( "/([^|]\n\r|\n|\r)+/s", '', $file );
		//	For Js W3C
		$file	=	preg_replace( "/(<script>)(.*?)(<\/script>)/s", "$1// <![CDATA[\n$2\n// ]]>$3", $file );

		/**
		 *	允许局部动态
		 *	HTML 调用语法
		 *	<!--{dynamic:文件名}-->
		 *		Code Here...
		 *	<!--{dynamic:end}-->
		 *	-----------------------------
		 *	原理：
		 *		将由dynamic包含的代码部分捕获出来
		 *		并生成新的php文件
		 *		然后通过createDynamicDate函数
		 *		将文件名生成指定格式的注释
		 */
		$file	=	preg_replace_callback( "/\<\?php\sdynamic\:(\w+)\?\>(.*?)\<\?php\sdynamic\:end\?\>/s", "createDynamicData", $file );

		@file_put_contents( $phpCacheFile, $file );
		return $phpCacheFile;
	}

	function	createDynamicData ( $matches )
	{
		$phpCacheFileName	=	ME_CACHE_DIR . '/__no-cache.' . $matches[1] . '.php';

		if( !file_exists( $phpCacheFileName ) )
			@file_put_contents( $phpCacheFileName, "<?php\nif( !defined( 'IN_TEAME' ) )\n{\n\texit( 'Access Denied' );\n}\n?>\n" . $matches[2] );

		return	"<!--{no-cache:'{$matches[1]}'}--><?php include '{$phpCacheFileName}'?><!--{no-cache:end}-->";
	}

	/**
	 *	这里生成指定的文件时将html代码替换成php文件
	 *	格式是：include '**.php'
	 *	-------------------------------------------------
	 *	具体生成情况可以参照./app/__Cache 和 ./app/__Archive 下
	 *	所生成的文件
	 */
	function	createNewArchiveFile ( $fileName, $string )
	{
		$file	=	$string;
		$file		=	preg_replace( "/\<\!\-\-\{no\-cache\:\'(\w+)\'\}\-\-\>(.*?)\<\!\-\-\{no\-cache\:end\}\-\-\>/s", "<?php include '" . ME_CACHE_DIR . '/__no-cache.' . "$1.php'?>", $file );
		@file_put_contents( $fileName, $file );
	}
?>